package test;

import de.ruedigermoeller.heapoff.structs.FSTStruct;
import de.ruedigermoeller.heapoff.structs.structtypes.StructString;
import de.ruedigermoeller.reallive.facade.database.RLTableConfiguration;
import de.ruedigermoeller.reallive.facade.database.RealLive;
import de.ruedigermoeller.reallive.impl.RLStructRow;
import de.ruedigermoeller.serialization.FSTClazzInfoRegistry;

import java.util.ArrayList;

/**
 * Copyright (c) 2012, Ruediger Moeller. All rights reserved.
 * <p/>
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * <p/>
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * <p/>
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 * <p/>
 * Date: 04.11.13
 * Time: 21:07
 * To change this template use File | Settings | File Templates.
 */
public class SampleSchema {

    public static void create(RealLive db) {
        db.createTable( new RLTableConfiguration(1,new User()));
        db.createTable( new RLTableConfiguration(2,new Session() ));
    }

    public static class ScrambledString extends StructString {

        public ScrambledString(int size) {
            super(size);
        }

        public ScrambledString(String init, int size) {
            super(init, size);
        }

        public ScrambledString(String s) {
            super(s);
        }

        public void scramble() {
            for ( int i = 0; i < getLen(); i++) {
                chars(i, (char) (chars(i) ^ 7777));
            }
        }

        public void unscrable() {
            scramble();
        }

    }

    public static class AuthData extends FSTStruct {

        protected ScrambledString name = new ScrambledString(20);
        protected ScrambledString password = new ScrambledString(8);

        public AuthData() {
        }

        public ScrambledString getName() {
            return name;
        }

        public void setName(ScrambledString name) {
            this.name = name;
        }

        public ScrambledString getPassword() {
            return password;
        }

        public void setPassword(ScrambledString password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "AuthData{" +
                    "name=" + name +
                    ", password=" + password +
                    '}';
        }
    }

    public static class User extends RLStructRow {

        protected AuthData auth = new AuthData();
        protected long created;
        protected long rights;
        protected StructString name = new StructString(20);
        protected StructString preName = new StructString(20);
        protected int age;

        public AuthData getAuth() {
            return auth;
        }

        public void setAuth(AuthData auth) {
            this.auth = auth;
        }

        public long getCreated() {
            return created;
        }

        public void setCreated(long created) {
            this.created = created;
        }

        public long getRights() {
            return rights;
        }

        public void setRights(long rights) {
            this.rights = rights;
        }

        public StructString getName() {
            return name;
        }

        public void setName(StructString name) {
            this.name = name;
        }

        public StructString getPreName() {
            return preName;
        }

        public void setPreName(StructString preName) {
            this.preName = preName;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "User{" +
                    "auth=" + auth +
                    ", created=" + created +
                    ", rights=" + rights +
                    ", name=" + name +
                    ", preName=" + preName +
                    ", age=" + age +
                    '}';
        }
    }

    public static class Session extends RLStructRow {

        protected User user = new User();
        protected long sessionCreation;

        public User getUser() {
            return user;
        }

        public void setUser(User user) {
            this.user = user;
        }

        public long getSessionCreation() {
            return sessionCreation;
        }

        public void setSessionCreation(long sessionCreation) {
            this.sessionCreation = sessionCreation;
        }

        @Override
        public String toString() {
            return "Session{" +
                    "user=" + user +
                    ", sessionCreation=" + sessionCreation +
                    '}';
        }

    }

    public static void main( String arg[]) {
        ArrayList<String> arr = new ArrayList<>();
        FSTClazzInfoRegistry.addAllReferencedClasses(SampleSchema.class,arr);
        System.out.println("POK");
    }

}
